package model

import (
	"langtu.com/common"
	"langtu.com/common/lang"
	"langtu.com/example/my-example/pkg"
	"slices"
)

type User struct {
	Id               int64
	CreateModifyTime *lang.CreateModifyTime
	Username         string
	Password         lang.Password
	State            int
}

// NewUser
//
//	@Description: 创建用户信息
//	@param username
//	@param password
//	@return *User
func NewUser(username string, password string) User {
	return User{
		CreateModifyTime: lang.NewCreateModifyTime(),
		Username:         username,
		Password:         lang.NewPassword(password),
		State:            pkg.UserStateActive,
	}
}

// UpdateStateCode
//
//	@Description:
//	@receiver u
//	@param state
//	@return error
func (u *User) UpdateStateCode(state int) error {
	if !slices.Contains([]int{pkg.UserStateActive, pkg.UserStateForbidden}, state) {
		return &common.IllegalArgumentError{}
	}
	u.CreateModifyTime.SetModifyTimeToNow()
	u.State = state
	return nil
}
